<?php
class BaseModel {
    
    // declare a variable to hold the database-class
    protected  $m_db ;
    
    /**
     * __construct
     * 
     * @param DbModel $a_db
     * 
     * @return NULL
     * 
     */
    public function __construct($a_db) {
        $this->m_db = $a_db ;
    }
    
    //--------------------
    //- CRUD - functions -
    //--------------------
    /**
     * create
     * 
     * @param array $a_boatInfo
     * @return boolean
     *      
     */
    public function create($a_info) {

        // create the object
        $class = $this::CLASS_NAME ;
        $object = new $class() ;

		
        // and give it some values
        $object->create($a_info) ;
        
        // add the object to the database via our orm
        if($this->m_db->addObjectToTable($this::TABLE_NAME, $object)) {
            
            return true ;       // return the object?
        }

        return false ;
        
    }
    
    /**
     * read
     * 
     * @param int $a_id[default] Send an id if you only want one member
     * 
     * @return MemberClass
     */
     public function read($a_id = null, $a_tableId = null) {
         
        if($a_tableId === null) {
            $a_tableId = $this::TABLE_ID ;
        }
        
        // Check if we only want one or the whole bunch
        if($a_id === null){
            $members = $this->m_db->get($this::TABLE_NAME, $this::CLASS_NAME);
        } else if (is_int($a_id)) {
            $members = $this->m_db->get($this::TABLE_NAME, $this::CLASS_NAME, 'WHERE '. $a_tableId .' = '.$a_id);
        } else {
			$members = $this->m_db->get($this::TABLE_NAME, $this::CLASS_NAME, 'WHERE '. $a_tableId .' = \''.$a_id.'\'');
		}

        return $members ;
            
    
    }
     /**
     * update
     * 
     * @param array $a_boatInfo
     * 
     * @return boolean
     */
    public function update($a_info) {

        // create the boat-object
        $class = $this::CLASS_NAME ;
        $object = new $class() ;
        
        // and give it some values
        $object->create($a_info) ;

        // And update
        if($this->m_db->update('WHERE '.$this::TABLE_ID.' = '.$object->getId(), $this::TABLE_NAME, $object)){
                
            return true;
        
        }
        
        return false;
    }
    
    /**
     * delete
     * 
     * @param int $a_id
     * 
     * @return boolean
     * 
     */
    public function delete($a_id) {
            
        if($this->m_db->delete("WHERE ".$this::TABLE_ID." = ".$a_id, $this::TABLE_NAME)){

            return true;
        }
        return false;
    } 
    
    public function addMessage($message) {
        MessageModel::addMessage($message) ;
    }
    
}
